package com.quectel.cms.controller.partybuildingmeeting;


import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.partybuildingmeeting.dto.PartyBuildingMeetingExperienceRecordDto;
import com.quectel.core.module.partybuildingmeeting.service.PartyBuildingMeetingExperienceRecordService;
import com.quectel.util.common.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2023/05/24 11:38
 */
@RestController
@RequestMapping("partyBuildingMeetingExperienceRecord")
@Tag(name = "PartyBuildingMeetingExperienceRecordController", description = "参会党员心得记录相关api")
public class PartyBuildingMeetingExperienceRecordController extends BaseController {

    @DubboReference
    private PartyBuildingMeetingExperienceRecordService partyBuildingMeetingExperienceRecordService;

    /**
     * 列表
     */
    @GetMapping("exportExcel")
    @RequirePermissions(values = "car:exportExcel")
    @Operation(summary = "导出心得列表")
    @Log(express = "导出心得列表")
    public void exportExcel(@RequestParam Map<String, Object> params, HttpServletResponse hresp) {
        Request request = Request.configParamsNotPage(params);

        super.fillAuthorityData(params);

        List<PartyBuildingMeetingExperienceRecordDto> result = new ArrayList<>();
        int pageNo = 0;
        params.put("limit", 1000);
        while (true) {
            params.put("offset", pageNo++ * 1000);
            List<PartyBuildingMeetingExperienceRecordDto> list = partyBuildingMeetingExperienceRecordService.queryList(request.getParams());
            if (org.apache.commons.collections4.CollectionUtils.isEmpty(list)) {
                break;
            }
            result.addAll(list);
            if (list.size() < 1000) {
                break;
            }
        }
        String[] headers = new String[]{"会议主题", "提交人", "联系电话","所属党组织","学习心得","提交时间"};
        List<String[]> dataList = new ArrayList<>();
        for (PartyBuildingMeetingExperienceRecordDto data : result) {
            String[] item = new String[headers.length];
            item[0] = data.getSubject();
            item[1] = data.getPartyPersonName();
            item[2] = data.getMobile();
            item[3] = data.getPartyName();
            item[4] = data.getContent();
            item[5] = DateUtils.format(data.getCreateTime(), DateUtils.FormatType.COMMON);
            dataList.add(item);
        }
        POIUtils.exportExcel(hresp, "参会记录", headers, dataList, "参会记录");
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("list")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:list")
    @Operation(summary = "分页获取参会党员心得记录列表")
    public Response<Page<PartyBuildingMeetingExperienceRecordDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = partyBuildingMeetingExperienceRecordService.queryTotal(request.getParams());

        List<PartyBuildingMeetingExperienceRecordDto> list = null;
        if (total > 0) {
            list = partyBuildingMeetingExperienceRecordService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<PartyBuildingMeetingExperienceRecordDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<PartyBuildingMeetingExperienceRecordDto>>ok().wrap(page);
    }

    /**
     * 编辑查询反显
     *
     * @param id
     * @return
     */
    @GetMapping("infoForEdit/{id}")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:edit")
    @Operation(summary = "在编辑时根据id查询参会党员心得记录详情")
    public Response<PartyBuildingMeetingExperienceRecordDto> infoForEdit(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingMeetingExperienceRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingMeetingExperienceRecordDto partyBuildingMeetingExperienceRecordDto = partyBuildingMeetingExperienceRecordService.selectById(id);

        return Response.<PartyBuildingMeetingExperienceRecordDto>ok().wrap(partyBuildingMeetingExperienceRecordDto);
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:info")
    @Operation(summary = "根据id查询参会党员心得记录的详情")
    public Response<PartyBuildingMeetingExperienceRecordDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (partyBuildingMeetingExperienceRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        PartyBuildingMeetingExperienceRecordDto partyBuildingMeetingExperienceRecordDto = partyBuildingMeetingExperienceRecordService.selectById(id);

        return Response.<PartyBuildingMeetingExperienceRecordDto>ok().wrap(partyBuildingMeetingExperienceRecordDto);
    }

    /**
     * 保存
     *
     * @param partyBuildingMeetingExperienceRecordDto
     * @return
     */
    @PostMapping("save")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:edit")
    @Operation(summary = "新增参会党员心得记录")
    public Response<Object> save(@RequestBody @Validated PartyBuildingMeetingExperienceRecordDto partyBuildingMeetingExperienceRecordDto) {

        //填充基础数据
        fillSaveCommonData(partyBuildingMeetingExperienceRecordDto);

        partyBuildingMeetingExperienceRecordService.save(partyBuildingMeetingExperienceRecordDto);

        return Response.ok();
    }


    /**
     * 修改
     *
     * @param partyBuildingMeetingExperienceRecordDto
     * @return
     */
    @PostMapping("update")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:edit")
    @Operation(summary = "根据id修改参会党员心得记录")
    public Response<Object> update(@RequestBody  @Validated PartyBuildingMeetingExperienceRecordDto partyBuildingMeetingExperienceRecordDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(partyBuildingMeetingExperienceRecordDto.getId()));
        if (partyBuildingMeetingExperienceRecordService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(partyBuildingMeetingExperienceRecordDto);

        partyBuildingMeetingExperienceRecordService.updateById(partyBuildingMeetingExperienceRecordDto);

        return Response.ok();
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    @RequirePermissions(values = "partyBuildingMeetingExperienceRecord:delete")
    @Operation(summary = "根据多条记录id批量删除参会党员心得记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (partyBuildingMeetingExperienceRecordService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        partyBuildingMeetingExperienceRecordService.deleteBatch(ids);

        return Response.ok();
    }
}
